package com.yztc.ui.home.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yztc.bean.home.ProductListBean;
import com.yztc.chunboapp.R;
import com.yztc.utils.ImageHelper;

import java.util.List;

/**
 * Created by My on 2016/10/8.
 */
public class HomeHRecycleViewAdapter extends RecyclerView.Adapter<HomeHRecycleViewAdapter.MyViewHolder> {
   private Context mContext;
   private List<ProductListBean> mProductList;
    //将定义事件的接口作为适配器的属性
   private OnItemClickListener mOnItemClickListener;
   public HomeHRecycleViewAdapter(Context mContext,List<ProductListBean>mProductList){
       this.mContext = mContext;
       this.mProductList = mProductList;
   }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate
                (R.layout.home_hengxiang_recycleview_item,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        ImageHelper.getNormalImage(mContext,mProductList.get(position).getUrl(),holder.ivUrl);
        holder.tvShortname.setText(mProductList.get(position).getShortname());
        holder.tvSubname.setText(mProductList.get(position).getSubname());
        holder.tvSpecifications.setText(mProductList.get(position).getSpecifications());
        String chunboPrice = mProductList.get(position).getChunboPrice();
        String discountPrice = mProductList.get(position).getDiscountPrice();
        if (chunboPrice.equals(discountPrice) || "0.00".equals(discountPrice)){
            holder.tvChunboPrice.setText("¥"+chunboPrice);
        }else {
            holder.tvChunboPrice.setText("¥"+chunboPrice);
            holder.tvChunboPrice.getPaint()
                    .setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
            holder.tvDiscountPrice.setText("¥"+discountPrice);
            holder.ivQiangGou.setImageResource(R.mipmap.xsqg);
        }
        //判断当前控件是否设置了监听,点击监听
        if (mOnItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(v,position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mProductList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView ivUrl,ivQiangGou;
        TextView tvShortname,tvSubname,tvSpecifications,tvDiscountPrice,
                tvChunboPrice;
        public MyViewHolder(View itemView) {
            super(itemView);
            ivUrl = (ImageView) itemView.findViewById(R.id.iv_url);
            ivQiangGou = (ImageView) itemView.findViewById(R.id.iv_qiangGou);
            tvShortname = (TextView) itemView.findViewById(R.id.tv_shortname);
            tvSubname = (TextView) itemView.findViewById(R.id.tv_subname);
            tvSpecifications = (TextView) itemView.findViewById(R.id.tv_specifications);
            tvChunboPrice = (TextView) itemView.findViewById(R.id.tv_chunbo_price);
            tvDiscountPrice = (TextView) itemView.findViewById(R.id.tv_discount_price);
        }
    }

   public interface OnItemClickListener{
        public void onItemClick(View view,int position);
    }
}
